package com.nimitz.web.controller.module.demo;

import com.nimitz.common.core.controller.BaseController;
import com.nimitz.common.core.domain.AjaxResult;
import com.nimitz.common.core.page.TableDataInfo;
import com.nimitz.common.utils.StringUtils;
import com.nimitz.module.demo.domain.DemUser;
import com.nimitz.module.demo.service.IDemUser2Service;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户维护进阶
 */
@RestController
@Api(tags = "演示模块-用户维护进阶")
@RequestMapping("/module/demo/user2")
public class DemUser2Controller extends BaseController {

    @Resource
    private IDemUser2Service userService;

    /**
     * 新增用戶
     *
     * @param brandName 品牌名称
     * @param user      用戶
     * @return 结果
     */
    @PostMapping
    @ApiOperation(value = "新增用户")
    // @Log(title = "用户管理", businessType = BusinessType.INSERT)
    public AjaxResult add(@RequestParam("brandName") String brandName, @RequestBody DemUser user) {
        user.setCreateBy("Vincent.Jin");
        return toAjax(userService.insertUser(brandName, user));
    }

    /**
     * 根据用户id删除用户
     *
     * @param brandName 品牌名称
     * @param userId    用户id
     * @return 结果
     */
    // @Log(title = "用户管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/")
    @ApiOperation(value = "根据用户id删除用户")
    public AjaxResult remove(@RequestParam("brandName") String brandName, @RequestParam("userId") Integer userId) {
        return toAjax(userService.deleteUserById(brandName, userId));
    }

    /**
     * 批量删除用戶
     *
     * @param brandName 品牌名称
     * @param userIds   需要删除的用戶id集合
     * @return 结果
     */
    // @Log(title = "用户管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/userIds/")
    @ApiOperation(value = "批量删除用户")
    public AjaxResult removes(@RequestParam("brandName") String brandName, @RequestParam("userIds") Integer[] userIds) {
        return toAjax(userService.deleteUserByIds(brandName, userIds));
    }

    /**
     * 修改用戶
     *
     * @param brandName 品牌名称
     * @param user      用戶
     * @return 结果
     */
    // @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "修改用戶")
    public AjaxResult edit(@RequestParam("brandName") String brandName, @RequestBody DemUser user) {
        user.setUpdateBy("Vincent.Jin");
        return toAjax(userService.updateUser(brandName, user));
    }

    /**
     * 查询所有用户
     *
     * @param brandName 品牌名称
     * @return 用戶集合
     */
    @GetMapping("/list/all")
    @ApiOperation(value = "查询所有用户")
    public TableDataInfo list(@RequestParam("brandName") String brandName) {
        startPage();
        List<DemUser> list = userService.selectUserAll(brandName);
        return getDataTable(list);
    }

    /**
     * 根据用戶id查询用戶
     *
     * @param brandName 品牌名称
     * @param userId    用戶id
     * @return 用戶
     */
    @GetMapping("/getInfo")
    @ApiOperation(value = "根据用戶id查询用戶")
    public AjaxResult getInfo(@RequestParam("brandName") String brandName, @RequestParam("userId") Integer userId) {
        AjaxResult ajax = AjaxResult.success();
        if (StringUtils.isNotNull(userId)) {
            DemUser demUser = userService.selectUserById(brandName, userId);
            ajax.put(AjaxResult.DATA_TAG, demUser);
        }
        return ajax;
    }

    /**
     * 根据条件查询用户列表（含分页）
     *
     * @param brandName 品牌名称
     * @param user      用戶
     * @return 用戶集合
     */
    @GetMapping("/list")
    @ApiOperation(value = "根据条件查询用户列表（含分页）")
    public TableDataInfo list(@RequestParam("brandName") String brandName, DemUser user) {
        startPage();
        List<DemUser> list = userService.selectUserList(brandName, user);
        return getDataTable(list);
    }
}
